from datetime import date

from django.http import HttpResponse
from django.shortcuts import render_to_response

from parse_today import births_of_the_day, transform_birth_infos

def first_function(request):
    return HttpResponse("Hello, world. Welcome!")

def get_todays_births(request):
    return get_births(request, date.today().strftime('%B_%d'))

def get_births(request, todays_date):

    birth_infos = births_of_the_day(todays_date)

    if birth_infos is None:
        return HttpResponse("Looks like something bad happened. Unable to fetch data. Please try later.")

    if birth_infos == 404:
        return HttpResponse("The page you requested is not found.")

    birth_info_templates = transform_birth_infos(birth_infos)

    if birth_info_templates is None:
        return HttpResponse("Looks like something bad happened. Unable to fetch data. Please try later.")
    else:
	day = todays_date.replace('_',' ')
	return render_to_response('births.html', {'birth_info_templates':birth_info_templates, 
	                	                  'day':day, 
						  'wiki_day':todays_date})
